using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using cframework_xlua;
using System.ComponentModel;
using Newtonsoft.Json;
using Aliyun.OSS.Samples;
using System.Net;
using System.IO;
namespace cframework_xlua
{
    [CreateAssetMenu(menuName = "cframework_xlua/FileSystemUtil_AliOSS")]
    public class FileSystemUtil_AliOSS : FileSystemUtil
    {
        [Essential]
        public string ossBucket;
        [Essential]
        public string ossEndPoint;
        [Essential]
        public string ossCdn;
        [Essential]
        public string ossAccessKeyId;
        [Essential]
        public string ossAccessKeySecret;

        public override string GetDownloadUrl()
        {
            return ossCdn;
        }
        public override bool Upload(string srcPath, string destPath)
        {
            if (System.IO.File.Exists(srcPath))
            {
                Aliyun.OSS.Samples.UploadFileToAliOssTool.Upload(ossAccessKeyId, ossAccessKeySecret, ossEndPoint, ossBucket, srcPath, destPath);
            }
            else
            {
                string[] paths = System.IO.Directory.GetFiles(srcPath, "*", System.IO.SearchOption.AllDirectories);
                foreach (var p in paths)
                {
                    var q = p.Replace("\\", "/").Replace(srcPath + "/", "");
                    Upload(p, $"{destPath}/{q}");
                }
            }
            return true;
        }

        public override bool WriteAllText(string path, string text)
        {
            PutObjectManager.Init(ossAccessKeyId, ossAccessKeySecret, ossEndPoint);
            var o = PutObjectManager.PutObjectFromString(ossBucket, text, path);
            return o != null;
        }
        public override string ReadAllText(string path)
        {
            PutObjectManager.Init(ossAccessKeyId, ossAccessKeySecret, ossEndPoint);
            return PutObjectManager.GetObjectToString(ossBucket, path);
        }
    }
}